<?php
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class UploadController extends AppController
{
    public $name = 'Upload';
    public $uses = array(
        'HeroStatInfoList', 'AccessoryInfoList', 'ActiveSkillInfoList',
        'ArmorInfoList', 'ChatActionInfoList', 'LevelExpInfoList',
        'MaterialInfoList', 'MonsterAllyInfoList', 'MonsterEnemyInfoList',
        'TalkInfoList', 'PassiveSkillInfoList', 'WeaponInfoList', 'WorldMapList',
        'WorldMapInfoList', 'AttrClassInfoList', 'FarmInfoList', 'FarmExpansionInfoList',
        'ActiveSkillLvInfoList', 'PassiveSkillLvInfoList', 'CrystalInfoList', 'GaChaInfoList',
        'DisbandInfoList', 'EvolveInfoList', 'ItemUpgradeInfoList', 'MapInfoList', 'SMapInfoList',
        'WMapInfoList', 'FarmSkillInfoList', 'FarmLevelExpInfoList', 'QuestInfoList', 'SystemMsg',
        'TipInfoList', 'ScenarioInfoList', 'FirstUserInfoList', 'CollectLevelExpInfoList',
        'CollectQuestInfoList', 'DailyRewardInfoList', 'CostumeInfoList', 'ProductInfoList',
        'SpecialMapInfoList', 'LabyrinthMapInfoList', 'GuildLevelInfoList', 'GuildBuffInfoList',
        'StateEffectInfoList', 'BuffInfoList', 'QuestItemInfoList',
        'W1MapInfoList', 'W2MapInfoList', 'W3MapInfoList', 'W4MapInfoList', 'W5MapInfoList',
        'PecoMessageInfoList', 'ClientLinkInfoList', 'MonthRewardInfoList'
    );

    public function index()
    {
        $header_name = $this->header_name;
        $this->set(compact(array('header_name')));

        if( !empty($this->data['Upload'] ))
        {
            $json_dir = new Folder(WWW_ROOT . 'json');
            if(!$json_dir->path)
            {
                $new_folder = new Folder();
                $new_folder->create(WWW_ROOT . 'json');
            }

            $file_array = $this->data['Upload']['files'];

            foreach($file_array as $num => $data)
            {
                $tmp_file = $data;
                // select file check
                if(!is_uploaded_file($tmp_file['tmp_name']))
                {
                    $this->set(compact(array('header_name')));
                    $this->Session->setFlash('파일을 선택하세요.', 'default', array('class' => 'message'), 'bad');
                    return ;
                }

                $file_name = basename($tmp_file['name']);
                $upload_dir = WWW_ROOT . 'json' . DS;
                $upload_file = $upload_dir . $file_name;

                // file check
                // application/octet-stream
                if($tmp_file['type'] != 'text/plain' && $tmp_file['type'] != 'application/octet-stream')
                {
                    $this->set(compact(array('header_name')));
                    $this->Session->setFlash('텍스트 파일이 아닙니다.', 'default', array('class' => 'message'), 'bad');
                    return ;
                }

                // upload check
                if(!move_uploaded_file($tmp_file['tmp_name'], $upload_file))
                {
                    $this->set(compact(array('header_name')));
                    $this->Session->setFlash('업로드 실패', 'default', array('class' => 'message'), 'bad');
                    return ;
                }

                $file_read = new File($upload_file);
                $json_data = $file_read->read(true, 'r');

                $result = json_decode($json_data, true);

                $collection_name = array_keys($result);
                $collection_name = $collection_name[0];

                foreach($result[$collection_name] as $data)
                {
                    // 가차 몬스터
                    if($collection_name == 'GaChaInfoList')
                    {
                        if($data['Id'] == 'Monster_Friend')
                        {
                            $check = idCheck($data['EquipGrade'], $this->db_name, $this->MonsterAllyInfoList);
                            foreach($check as $check_data)
                            {
                                if(empty($check_data['data']))
                                {
                                    $this->set(compact(array('header_name')));
                                    $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                    return ;
                                }
                            }
                        }

                        if($data['Id'] == 'Monster_Crystal')
                        {
                            $check = idCheck($data['EquipGrade'], $this->db_name, $this->MonsterAllyInfoList);
                            foreach($check as $check_data)
                            {
                                if(empty($check_data['data']))
                                {
                                    $this->set(compact(array('header_name')));
                                    $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                    return ;
                                }
                            }
                        }
                    }

                    // 맵 체크
                    /*if($collection_name == 'W1MapInfoList' || $collection_name == 'W2MapInfoList' ||
                        $collection_name == 'W3MapInfoList'|| $collection_name == 'W4MapInfoList' ||
                        $collection_name == 'W5MapInfoList')
                    {

                        $monsters = array();
                        $monsters[]['Id'] = $data['Boss']['Boss'];
                        foreach($data['Monsters'] as $mon_id)
                        {
                            $monsters[]['Id'] = $mon_id;
                        }

                        foreach($data['Boss']['Monsters'] as $mon_id)
                        {
                            $monsters[]['Id'] = $mon_id;
                        }

                        $check = idCheck($monsters, $this->db_name, $this->MonsterAllyInfoList);
                        foreach($check as $check_data)
                        {
                            if(empty($check_data['data']))
                            {
                                $this->set(compact(array('header_name')));
                                $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                return ;
                            }
                        }

                        // 드랍 재료 아이템 이름
                        if(!empty($data['FieldDropData']))
                        {
                            if(!empty($data['FieldDropData']['HerbData']))
                            {
                                $check = idCheck($data['FieldDropData']['HerbData'], $this->db_name, $this->MaterialInfoList);
                                foreach($check as $check_data)
                                {
                                    if(empty($check_data['data']))
                                    {
                                        $this->set(compact(array('header_name')));
                                        $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                        return ;
                                    }
                                }
                            }

                            if(!empty($data['FieldDropData']['StoneData']))
                            {
                                $check = idCheck($data['FieldDropData']['StoneData'], $this->db_name, $this->MaterialInfoList);
                                foreach($check as $check_data)
                                {
                                    if(empty($check_data['data']))
                                    {
                                        $this->set(compact(array('header_name')));
                                        $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                        return ;
                                    }
                                }
                            }

                            if(!empty($data['FieldDropData']['BoxData']))
                            {
                                $check = idCheck($data['FieldDropData']['BoxData'], $this->db_name, $this->MaterialInfoList);
                                foreach($check as $check_data)
                                {
                                    if($check_data['id'] != 'Gold' && $check_data['id'] != 'Mimic')
                                    {
                                        if(empty($check_data['data']))
                                        {
                                            $this->set(compact(array('header_name')));
                                            $this->Session->setFlash($file_name . ' : ' . $check_data['id'] . '없음', 'default', array('class' => 'message'), 'bad');
                                            return ;
                                        }
                                    }
                                }
                            }
                        }
                    }*/
                }

                if(!$this->$collection_name->set_all_data($this->db_name, $result[$collection_name]))
                {
                    $this->set(compact(array('header_name')));
                    $this->Session->setFlash('디비 업로드 실패', 'default', array('class' => 'message'), 'bad');
                    return ;
                }

                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();

                //$url = $this->server_url . '/data_reset';
                $url = $this->server_url . '/game_data_reset';
                $data = array('name' => $collection_name);

                if(!$HttpSocket->post($url, $data))
                {
                    return 'error!!!';
                }
            }

            /*$tmp_file = $this->data['Upload']['thumb_file'];

            // select file check
            if(!is_uploaded_file($tmp_file['tmp_name']))
            {
                $this->Session->setFlash('파일을 선택하세요.', 'default', array('class' => 'message'), 'bad');
                return ;
            }

            $file_name = basename($tmp_file['name']);
            $upload_dir = WWW_ROOT . 'json' . DS;
            $upload_file = $upload_dir . $file_name;

            // file check
            // application/octet-stream
            if($tmp_file['type'] != 'text/plain' && $tmp_file['type'] != 'application/octet-stream')
            {
                $this->Session->setFlash('텍스트 파일이 아닙니다.', 'default', array('class' => 'message'), 'bad');
                return ;
            }

            // upload check
            if(!move_uploaded_file($tmp_file['tmp_name'], $upload_file))
            {
                $this->Session->setFlash('업로드 실패', 'default', array('class' => 'message'), 'bad');
                return ;
            }

            $file_read = new File($upload_file);
            $json_data = $file_read->read(true, 'r');

            $result = json_decode($json_data, true);

            $collection_name = array_keys($result);
            $collection_name = $collection_name[0];

            if(!$this->$collection_name->set_all_data($this->db_name, $result[$collection_name]))
            {
                $this->Session->setFlash('디비 업로드 실패', 'default', array('class' => 'message'), 'bad');
                return ;
            }

            App::uses('HttpSocket', 'Network/Http');
            $HttpSocket = new HttpSocket();

            $url = $this->server_url . '/data_reset';
            $data = '';

            if(!$HttpSocket->post($url, $data))
            {
                return 'error!!!';
            }*/
            $this->set(compact(array('header_name')));
            $this->Session->setFlash('디비 업로드 성공~', 'default', array('class' => 'success'), 'good');
        }
    }
}